package com.waves.task.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.waves.core.utils.StringUtils;
import com.waves.task.domain.dto.TaskPageDTO;
import com.waves.task.domain.entity.Task;
import com.waves.task.domain.enums.TaskTypeEnum;
import com.waves.task.mapper.TaskMapper;
import org.springframework.stereotype.Service;


@Service
public class TaskDao extends ServiceImpl<TaskMapper, Task> {

    public Page<Task> listTaskPage(TaskPageDTO dto) {
        LambdaQueryWrapper<Task> wrapper = new LambdaQueryWrapper<Task>()
                .in(CollUtil.isNotEmpty(dto.getTaskIds()), Task::getId, dto.getTaskIds())
                .eq(StringUtils.isNotEmpty(dto.getTitle()), Task::getTitle, dto.getTitle())
                .in(CollUtil.isNotEmpty(dto.getUserIds()), Task::getUserId, dto.getUserIds())
                .isNotNull(TaskTypeEnum.TASK_TYPE_IMG.getType().equals(dto.getType()), Task::getImgUrl)
                .ge(TaskTypeEnum.TASK_TYPE_HOT.getType().equals(dto.getType()), Task::getLikedNum, 1)
                .or().eq(dto.getType() <= 2 || dto.getType() == 10, Task::getType, dto.getType())
                .orderByDesc(Task::getCreatedTime);
        return baseMapper.selectPage(new Page<>(dto.getPageNum(), dto.getPageSize()), wrapper);
    }
}
